package com.lifesaftypower.activity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;

import com.lifesaftypower.adapter.ProductSectionAdapter;
import com.lifesaftypower.entity.Product;

public class ProductListActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_product_list);

		ExpandableListView lvProduct = (ExpandableListView) findViewById(R.id.lvProduct);
		List<List<Product>> productList = new ArrayList<List<Product>>();
		String[] sectors = Product.getSector();
		for (String sec : sectors) {
			productList.add(Arrays.asList(Product.getBySector(sec)));
		}

		final ProductSectionAdapter adapter = new ProductSectionAdapter(this,
				productList);
		lvProduct.setAdapter(adapter);

		// expend all group
		int count = adapter.getGroupCount();
		for (int position = 1; position <= count; position++) {
			lvProduct.expandGroup(position - 1);
		}

		// disable group collapsed
		lvProduct.setOnGroupClickListener(new OnGroupClickListener() {
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				return true; // This way the expander cannot be collapsed
			}
		});
		
		
		lvProduct.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				
				Intent intent = new Intent(ProductListActivity.this, ProductDetailActivity.class);
				intent.putExtra(ProductDetailActivity.ARGS_MODEL, ((Product)(adapter.getChild(groupPosition, childPosition))).getModel());
				startActivity(intent);
				return true;
			}
			
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_product_list, menu);
		return true;
	}

}
